﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvDTE;
using System.Collections;

namespace SPVisualDev.VSAddin.UserControls
{
    internal partial class UCProjectExplorer : UserControl
    {
        private Project m_startup_project;
        public delegate void TreeNodeVisitor(TreeNode node);

        public UCProjectExplorer()
        {
            InitializeComponent();

            this.ShowFolders = true;
        }

        public ProjectItem RootFolder
        {
            get;
            set;
        }

        public TreeView TreeView
        {
            get { return ProjectTreeView; }
        }

        public bool ShowFolders
        {
            get;
            set;
        }

        public bool ShowFiles
        {
            get;
            set;
        }


        public void Initialize(Project ParentProject)
        {
            m_startup_project = ParentProject;

            ProjectTreeView.Nodes.Clear();
            ProjectTreeView.TreeViewNodeSorter = new ItemNodeComparer();
        }

        public void RenderRootNode()
        {
            TreeNode node;
            ProjectItems collection;

            ProjectTreeView.Nodes.Clear();
            
            if (RootFolder == null)
            {
                node = new TreeNode(m_startup_project.Name);
                node.Name = m_startup_project.Name;
                node.Tag = m_startup_project;
                collection = m_startup_project.ProjectItems;
            }
            else
            {

                node = new TreeNode(RootFolder.Name);
                node.Name = RootFolder.Name;
                node.Tag = RootFolder;

                collection = RootFolder.ProjectItems;
            }

            node.ImageKey = "Folder";
            node.SelectedImageKey = "Folder";
            node.Expand();


            if (this.ShowFolders)
            {
                ProcessUIHierarchy(node, collection);

                ProjectTreeView.BeginUpdate();

                ProjectTreeView.Nodes.Add(node);
                ProjectTreeView.TopNode = node;

                ProjectTreeView.EndUpdate();
            }
        }

        private void ProcessUIHierarchy(TreeNode node, ProjectItems items)
        {
            foreach (ProjectItem item in items)
            {
                bool show;
                show = (item.Kind == Constants.vsProjectItemKindPhysicalFolder && this.ShowFolders);
                show = show | (item.Kind == Constants.vsProjectItemKindPhysicalFile && this.ShowFiles);

                if (!show)
                    continue;

                if (item.Kind == Constants.vsProjectItemKindPhysicalFolder ||
                    item.Kind == Constants.vsProjectItemKindVirtualFolder)
                {
                    TreeNode new_node = new TreeNode(item.Name);
                    new_node.Name = node.Name + "\\" + item.Name;
                    new_node.Tag = item;
                    new_node.ImageKey = "Folder";
                    new_node.SelectedImageKey = "Folder";

                    ProcessUIHierarchy(new_node, item.ProjectItems);

                    node.Nodes.Add(new_node);
                }
                else
                {
                    TreeNode new_node = node.Nodes.Add(node.Name + "\\" + item.Name, item.Name);
                    new_node.Tag = item;
                    new_node.ImageKey = "File";
                }
            }
        }

        public void ForEachNode(TreeNodeVisitor visitor, TreeNode StartNode)
        {
            if (StartNode == null)
                RecursiveVisistNodes(TreeView.TopNode, visitor);
            else
                RecursiveVisistNodes(StartNode, visitor);

        }

        private void RecursiveVisistNodes(TreeNode node, TreeNodeVisitor visitor)
        {
            visitor(node);

            foreach (TreeNode n in node.Nodes)
            {
                visitor(n);

                foreach (TreeNode sub in n.Nodes)
                {
                    RecursiveVisistNodes(sub, visitor);
                }
            }
        }



        private class ItemNodeComparer : IComparer
        {
            public int Compare(object x, object y)
            {
                ProjectItem itemX = (x as TreeNode).Tag as ProjectItem;
                ProjectItem itemY = (y as TreeNode).Tag as ProjectItem;

                if (itemX == null)
                    return -1;

                if (itemY == null)
                    return 1;

                if (itemX.Kind == Constants.vsProjectItemKindPhysicalFolder ||
                    itemX.Kind == Constants.vsProjectItemKindVirtualFolder)
                {
                    if (itemY.Kind == Constants.vsProjectItemKindPhysicalFolder ||
                        itemY.Kind == Constants.vsProjectItemKindVirtualFolder)
                        return string.Compare(itemX.Name, itemY.Name);
                    else
                        return -1;
                }
                else
                {
                    if (itemY.Kind != Constants.vsProjectItemKindPhysicalFolder &&
                        itemY.Kind != Constants.vsProjectItemKindVirtualFolder)
                        return string.Compare(itemX.Name, itemY.Name);
                    else
                        return 1;
                }
            }
        }
    }


}
